(* ===================================================================== *)
(** bopprocess : Interpreteur Processeur SSA -> application pipe01
    @author Mathieu Barbin *)
(* ===================================================================== *)

(** option -N est l'architecture *)

open Bplib 
open Bopprocesstools

let version = "1.0"

(* ===================================================================== *)
(** Parser le source *)
(* ===================================================================== *)
let parse_code_of_file filename =
  try
    index_ligne_net_list := 1;
    let ic = open_in filename in
    let buff = Lexing.from_channel ic in
    let youpi = ParserSSA.net_list LexerSSA.lexem_list buff
    in
    begin
      close_in ic;
      youpi
    end
  with
  | Sys_error _ -> 
      Printf.fprintf stderr "[!] '%s' : Not such file\n" filename;
      exit 1;
      failwith "w"
  | Parsing.Parse_error ->
      Printf.fprintf stderr "[!] line %d : Syntax Error\n" !index_ligne_net_list;
      exit 1;
      failwith "w"
  | Failure m ->
      Printf.fprintf stderr "[!] line %d : \"%s\"\n" !index_ligne_net_list m;
      exit 1;
      failwith "w"
(* ===================================================================== *)


(* ===================================================================== *)
(** Lecture des arguments systemes *)
(* ===================================================================== *)
let args_assoc = assoc_of_sys_argv Sys.argv
let get_arg_int_value = try_get_arg_int_value args_assoc
let get_arg_string_value = try_get_arg_string_value args_assoc
(* ===================================================================== *)

let _ =
  Printf.fprintf stderr "This is bopprocess @beboptools, version %s\n" version;
  (* Architecture *)
  let n = get_arg_int_value "-N" in
  let file = get_arg_string_value "-f" in
  (* On produit sur stdout ou dans un fichier *)
  let parse_code = parse_code_of_file file in
  let (env, code) = code_of_parse_code Prim.prims n parse_code in
  exec_code env code
   
